﻿#include "natiavparentwidget.h"
#include "natiavwidget.h"
#include "scanwidget.h"
#include "antivirus/antiviruswork.h"
#include "threatsoperationthread.h"

#define LEFT_MARGINS 0 //左边距
#define TOP_MARGINS 0 //上边距
#define RIGHT_MARGINS 0 //右边距离
#define BUTTOM_MARGINS 0 //下边距离
#define DIALOG_WIDTH 380 //dialog  宽度
#define DIALOG_HEIGHT 145 //dialog  高度
#define DIALOG_PB_WIDTH 140 //dialog  按钮宽度
#define DIALOG_PB_HEIGHT 36 //dialog  按钮高度

#define SET_ACC_NAME(contral, name) SET_ACCESS_NAME(contral, natiavParentWidget, name)

NatiavParentWidget::NatiavParentWidget(AntiVirusWork *work, QWidget *parent)
    : QWidget(parent)
    , m_stackWidget(nullptr)
    , m_scan(nullptr)
    , m_natiav(nullptr)
    , m_antivirusWork(work)
    , m_threatOperation(nullptr)
{
    setObjectName("NatiavParentWidget");
    m_natiav = new NatiavWidget(work, this);
    SET_ACC_NAME(m_natiav, natiavWidget);
    // init stack widget
    initGUI();
    m_stackWidget->setCurrentIndex(0);

    // mxz 对异常数据操作　单独开辟一个处理线程
    m_threatOperation = new ThreatsOPerationThread();
    m_threatOperation->moveToThread(&m_thread);
    connect(&m_thread, SIGNAL(finished()), &m_thread, SLOT(deleteLater()));
    // 处理异常数据
    connect(this, SIGNAL(notifyGetThreatsFile()), m_threatOperation, SLOT(getThreatFileList()));
    // 获得异常数据
    connect(m_threatOperation, SIGNAL(sendThreatsFileList(QString)), this, SLOT(onSendThreatsFileList(QString)));

    // 修复
    connect(m_threatOperation, SIGNAL(onAllRepairProblemFile(QString)), m_antivirusWork, SLOT(requestRepairProblemFile(QString)));
    // 隔离
    connect(m_threatOperation, SIGNAL(onAllIsolationFile(bool, QString)), m_antivirusWork, SLOT(requestChangeIsolationFile(bool, QString)));
    // 信任
    connect(m_threatOperation, SIGNAL(onAllTruestFile(bool, QString)), m_antivirusWork, SLOT(requestChangeTrustFiles(bool, QString)));
    m_thread.start();

    //跳转到扫描页面
    connect(m_natiav, SIGNAL(onNotifyShowScan(int,QString,QString)), this, SLOT(onNotifyShowScan(int, const QString &, const QString &)));

    // 创建扫描页
    initScanWidget();
}

//初始化界面
void NatiavParentWidget::initGUI()
{
    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->setContentsMargins(LEFT_MARGINS, TOP_MARGINS, RIGHT_MARGINS, BUTTOM_MARGINS);
    setLayout(mainLayout);

    m_stackWidget = new QStackedWidget;
    SET_ACC_NAME(m_stackWidget, stackWidget);
    m_stackWidget->show();
    mainLayout->addWidget(m_stackWidget);

    QWidget *wid1 = new QWidget;
    SET_ACC_NAME(wid1, stackWidgetWidget1);
    QWidget *wid2 = new QWidget;
    SET_ACC_NAME(wid2, stackWidgetWidget2);
    m_stackWidget->addWidget(wid1);
    m_stackWidget->addWidget(wid2);
}

void NatiavParentWidget::initScanWidget()
{
    m_scan = new ScanWidget(m_antivirusWork, this);
    SET_ACC_NAME(m_scan, scanWidget);

    // 再次扫描
    connect(m_scan, SIGNAL(scanAgain(int, const QString &, const QString &)), this, SLOT(onNotifyShowScan(int, const QString &, const QString &)));

    //返回信号处理
    connect(m_scan, SIGNAL(onBackpage()), this, SLOT(onBackpage()));

    // 更新上次扫描时间
    connect(this, SIGNAL(onUpdateScanTime()), m_antivirusWork, SLOT(updateLastScanningTime()));

    // 添加没有处理的异常数据
    connect(m_scan, SIGNAL(onAddThreatsFile(QString)), m_threatOperation, SLOT(addThreatsFile(QString)));
    // 准备移除的异常数据
    connect(m_scan, SIGNAL(readRemoveThreatsFile(QString)), m_threatOperation, SLOT(deleteThreatsFile(QString)));
    // 批量处理文件  给线程处理
    connect(m_scan, SIGNAL(senBatchOperation(QStringList,int)), m_threatOperation, SLOT(batchOPeration(QStringList,int)));
}

NatiavParentWidget::~NatiavParentWidget()
{
    m_threatOperation->deleteLater();
    m_threatOperation = nullptr;

    m_thread.quit();
    m_thread.wait();
}

void NatiavParentWidget::deleteScanWidget()
{
    // 扫描页删除之前, 存储危险数据
    threatOperation();
    disconnect(m_scan);
    m_scan->hide();
    m_scan->setParent(nullptr);
    m_scan->deleteLater();
    m_scan = nullptr;
    m_stackWidget->setCurrentIndex(0);
}

// 扫描通过类型路径，路径和版本
void NatiavParentWidget::scanByTypeAndPath(int nType, QString sPath, QString sVersion)
{
    this->setCurrentPage(1);
    m_scan->setType(nType, sPath, sVersion);
    //　扫描方法　通过不同的类型　0代表全盘
    if (nType == 0) {
        m_antivirusWork->doDissection(true, sPath);
    } else {
        m_antivirusWork->doDissection(false, sPath);
    }
}

void NatiavParentWidget::onSendThreatsFileList(QString sendpath)
{
    this->setCurrentPage(1);
    // 获取本地病毒库版本
    QString version = m_antivirusWork->localVdbVersion();
    m_scan->setType(2, sendpath, version);
    m_antivirusWork->doDissection(false, sendpath);
}

void NatiavParentWidget::onNotifyShowScan(int type, const QString &path, const QString &ver)
{
    Q_UNUSED(ver);
    //m_antivirusWork->showScanFromMainWindow(type, path);
    scanByTypeAndPath(type, path, m_antivirusWork->localVdbVersion());
}

void NatiavParentWidget::onBackpage()
{
    // 返回扫描主页同时  并更新上次扫描时间
    Q_EMIT onUpdateScanTime();
    this->setCurrentPage(0);
}

// 查杀为处理的异常文件
void NatiavParentWidget::scanThreatsFiles()
{
    emit notifyGetThreatsFile();
}

void NatiavParentWidget::setWork(AntiVirusWork *work)
{
    m_antivirusWork = work;
}

void NatiavParentWidget::setCurrentPage(int nPage)
{
    // 若已处于当页, 则不进行页面切换操作
    if (nPage == m_stackWidget->currentIndex()) {
        return;
    }

    // 设置 和 通知切换页面
    switch (nPage) {
    case 0:
        // 打开病毒查杀首页
        m_stackWidget->setCurrentIndex(0);
        // 通知module: 打开病毒查杀首页,并删除扫描页
        Q_EMIT notifyOpenAntiVirusHomePage();
        break;
    case 1:
        // 打开扫描页
        m_stackWidget->setCurrentIndex(1);

        // 通知module: 打开扫描页
        Q_EMIT notifyOpenScanVirusPage();
        break;
    default:
        // 打开病毒查杀首页
        m_stackWidget->setCurrentIndex(0);
        // 通知module: 打开病毒查杀首页,并删除扫描页
        Q_EMIT notifyOpenAntiVirusHomePage();
        break;
    }
}

// 扫描指定路径下的病毒
void NatiavParentWidget::scanVirusByPath(QString sPath)
{
    // 获取本地病毒库版本
    QString version = m_antivirusWork->localVdbVersion();
    scanByTypeAndPath(VirusScanType::CustomizedScan, sPath, version);
}

// 获得病毒查杀当前的页面
int NatiavParentWidget::getCurrentPage()
{
    return m_stackWidget->currentIndex();
}

// 是否有没有处理的风险异常
bool NatiavParentWidget::isThreats()
{
    return m_scan->finishAllOperation();
}

void NatiavParentWidget::threatOperation()
{
    m_scan->addThreats();
}
